import style from "./css.css";
import {SubmitButtonService} from "./service";

const SUBMIT_BUTTON_STATE_CODE = {
    CAN_NOT_SUBMIT: 'can_not_submit', //不可提交状态
    CAN_SUBMIT: 'can_submit', //正常可提交状态
    CAN_MODIFY: 'can_modify', //可修改状态
    RESET: 'reset', //重新作答
    FINISH: 'finish', //结束状态
    SUBMITTING: 'submitting', // 提交中状态，不可修改
    RETRY_SUBMIT: 'retry_submit', // 提交失败，重新提交
    MODIFY_SUBMIT: 'modify_submit'//修改后，重新提交
};

@Midway.MidwayModule({
    code: "InteractionSubmitButton",
    template: require('./view.html'),
    style: style,
    dependLibs: ['knockout']
})
class InteractionSubmitButton extends CommonClass {

    constructor() {
        super();
        this.metaInfo.name = 'InteractionSubmitButton';
        this.metaInfo.type = ConstVariable.PresenterType.FUNCTIONAL;
        this.currentState = SUBMIT_BUTTON_STATE_CODE.CAN_NOT_SUBMIT;
        this.isMouseDown = false;
        this.isAllowSubmit = false;
        this.result_tip = "";
        this.tType = "pptshell";
        this.languageKeySet = {};
        this.service = undefined;
    }

    getService() {
        if(this.service === undefined) {
            this.service = new SubmitButtonService(this);
        }
        return this.service;
    }

    $launch() {
        this.$view = $(this.View);
        this.linkQuestionId = this.Model["linkQuestionId"];
        this.linkQuestionType = this.Model["linkQuestionType"];
        this.canModify = this.Model["can_modify"];
        this.view_type = this.Model["view_type"];
        var tType = "new_em_button";
        if (this.view_type || this.isPPTShell) {
            tType = "new_em_button";
        } else if (this.isStudent || this.isWeb) {
            tType = "pad";
        }
        this.Logger.debug("InteractionSubmitButton tType " + tType);
        switch (tType) {
            case "pad": {
                this.tType = "pad";
                this.$view.find('.pad_button').css('display', '');
                break;
            }
            case "new_em_button": {
                this.tType = "new_em_button";
                this.$view.find('.pptshell_button').css('display', '');
                this.$view.find('.com_btn_round').addClass('com_btn_disabled');
                this.$view.find('.text_box').text(this._getLanguageText('submit_it'));
                break;
            }
        }
    }

    pageShow() {
        this._bindEvent();
    }

    $release() {
        this._unbindEvent();
        this.service = null;
    }

    _bindEvent() {
        if (this.tType == 'pad') {
            this.$view.find('#answer-question-submit')
                .on('mousedown', $.proxy(this._onButtonMouseDown, this))
                .on('mouseup', $.proxy(this._onButtonMouseUp, this));
        } else if (this.tType == 'new_em_button') {
            this.$view.find('.js_commitbutton_ppt')
                .on('mousedown touchstart', $.proxy(this._onButtonMouseDown, this))
                .on('mouseup touchend', $.proxy(this._onButtonMouseUp, this));
        }
    }

    _unbindEvent() {
        if (this.tType == 'pad') {
            this.$view.find('#answer-question-submit').off();
        } else if (this.tType == 'new_em_button') {
            this.$view.find('.js_commitbutton_ppt').off();
        }
    }

    _stateChanged(stateCode, isRecover) {
        this.currentState = stateCode;
        this.Logger.debug("InteractionSubmitButton stateChanged " + stateCode);
        var buttonElement = this.$view.find('.com_btn_round ');
        var buttonTextElement = this.$view.find('.text_box');
        if (this.tType == "pad") {
            buttonElement = this.$view.find('#answer-question-submit');
            buttonTextElement = this.$view.find('#answer-question-submit');
        }
        switch (this.currentState) {
            case SUBMIT_BUTTON_STATE_CODE.CAN_NOT_SUBMIT:
                buttonTextElement.text(this._getLanguageText('submit_question'));
                buttonElement.addClass('com_btn_disabled');
                this.isAllowSubmit = false;
                break;
            case SUBMIT_BUTTON_STATE_CODE.CAN_SUBMIT:
                buttonTextElement.text(this._getLanguageText('submit_question'));
                buttonElement.removeClass('com_btn_disabled');
                this.isAllowSubmit = true;
                break;
            case SUBMIT_BUTTON_STATE_CODE.RESET:
                buttonTextElement.text(this._getLanguageText('answer_again'));
                buttonElement.removeClass('com_btn_disabled');
                this.isAllowSubmit = true;
                break;
            case SUBMIT_BUTTON_STATE_CODE.CAN_MODIFY:
                if (this.canModify) {
                    buttonTextElement.text(this._getLanguageText('modify_answer'));
                    buttonElement.removeClass('com_btn_disabled');
                    this.isAllowSubmit = true;
                    break;
                } else {
                    this._stateChanged(SUBMIT_BUTTON_STATE_CODE.FINISH);
                    break;
                }
            case SUBMIT_BUTTON_STATE_CODE.FINISH:
                buttonTextElement.text(this._getLanguageText('has_ended'));
                buttonElement.addClass('com_btn_disabled');
                this.isAllowSubmit = false;
                break;
            case SUBMIT_BUTTON_STATE_CODE.SUBMITTING:
                buttonTextElement.text(this._getLanguageText('in_submit'));
                buttonElement.addClass('com_btn_disabled');
                this.isAllowSubmit = false;
                break;
            case SUBMIT_BUTTON_STATE_CODE.RETRY_SUBMIT:
                buttonTextElement.text(this._getLanguageText('reanswer_question'));
                buttonElement.removeClass('com_btn_disabled');
                this.isAllowSubmit = true;
                break;
            case SUBMIT_BUTTON_STATE_CODE.MODIFY_SUBMIT:
                buttonTextElement.text(this._getLanguageText('reanswer_question'));
                buttonElement.addClass('com_btn_disabled');
                this.isAllowSubmit = false;
                break;
        }
        buttonElement.attr('data-action', this.currentState);
        if (isRecover) {
            return;
        }
    }

    _tipChanged(tip) {
        if (tip) {
            this.$view.find(".submit-wrapper .tip").text(tip);
        }
    }

    _renderTime(time) {
        var returnTime = Math.ceil(time / 1000);
        if (returnTime < 60) {
            return returnTime + "秒前"
        }
        returnTime = Math.round(returnTime / 60);
        if (returnTime < 60) {
            return returnTime + "分钟前";
        }
        var hours = Math.round(returnTime / 60);
        var minute = returnTime % 60;
        //此处需要注意，语法可能会不同
        return hours + "小时" + minute + "分钟前";
    }

    //暂时不用
    _changeTipType(changeType) {
        var type = this.getType();
        if (changeType) {
            type = this._verifyType(changeType);
            this.set(type);
        }
        var text = '';
        var duration = this._getTime();
        switch (type) {
            case 'lastSubmit':
                text = '上次提交时间：' + this._renderTime(duration);
                break;
            case 'success':
                text = '提交成功';
                break;
            case 'failed':
                text = '提交失败';
                break;
            case 'blank':
                text = '';
                break;
            default:
                text = '';
        }
        this._tipChanged(text);
    }

    /*********以下是事件处理函数**************/
    _onButtonMouseDown(event) {
        this.Logger.debug("InteractionSubmitButton onButtonMouseDown " + this.isAllowSubmit);
        event.preventDefault();
        this.isMouseDown = false;
        if (this.isAllowSubmit) {
            this.isMouseDown = true;
        }
    }

    _onButtonMouseUp(event) {
        this.Logger.debug("InteractionSubmitButton onButtonMouseUp " + this.isMouseDown);
        event.preventDefault();
        event.stopPropagation();

        //支持作文提纲教学中的特性：未查看检查清单下点击提交按钮弹出提示框，所以需要这里发出事件通知(临时方案待后续重构调整)
        if (this.currentState == SUBMIT_BUTTON_STATE_CODE.CAN_NOT_SUBMIT) {
            this.dispatchEvent('Try2SubmitWhenDisabled', ConstVariable.PresenterEventType.IC_PLAYER_EVENT, {});
        }

        var self = this;
        if (self.isMouseDown) {
            if (this.currentState == SUBMIT_BUTTON_STATE_CODE.CAN_MODIFY) {
                this.Logger.debug("InteractionSubmitButton onButtonMouseUp currentState" + this.currentState);
                this._stateChanged(SUBMIT_BUTTON_STATE_CODE.MODIFY_SUBMIT);
                //题目作答状态改变，按钮变为重新提交，但不可点击
                this.dispatchEvent('questionStateChange',
                    ConstVariable.PresenterEventType.IC_PLAYER_EVENT, {
                        linkQuestionId: self.linkQuestionId,
                        canSubmit: false
                    });
            } else if (self.isAllowSubmit) {
                this.Logger.debug("InteractionSubmitButton onButtonMouseUp send custom_event_submit_answer");
                //发出提交按钮的事件
                self.isAllowSubmit = false;
                self.dispatchEvent('custom_event_submit_answer',
                    ConstVariable.PresenterEventType.IC_PLAYER_EVENT, {
                        question_id: self.linkQuestionId,
                        question_type: self.linkQuestionType,
                        state: self.currentState
                    });

                //体验优化(学生端)：不等待业务组件的业务数据，立即对用户点击做出UI响应
                // if(self.isStudent) {
                // 	self._stateChanged(SUBMIT_BUTTON_STATE_CODE.SUBMITTING, true);
                // }
            }
        }
    }

    _getLanguageText(key) {
        if (this.languageKeySet[key] == undefined)
            return "ERROR";
        return this.languageKeySet[key];
    }
}